package com.cskaoyan.controller;

import com.cskaoyan.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

//在我们的扫描包范围
@Controller
public class HelloController {

    @RequestMapping("/hello/view")
    public ModelAndView view() {
        //SpringMVC默认支持的视图：jsp
        //return new ModelAndView("/hello.jsp");
        ModelAndView modelAndView = new ModelAndView();
        //view对应的是视图名
        modelAndView.setViewName("/hello.jsp");
        //model是为视图提供参数的
        modelAndView.addObject("songge", "niupi");
        return modelAndView;
    }

    //@Autowired
    //UserService userService;

    //响应Json需要使用到@ResponseBody
    @RequestMapping("/hello/json")
    @ResponseBody
    public User json() {
        User user = new User();
        user.setUsername("songge");
        user.setPassword("niupi");
        return user;//返回值为user对象，SpringMVC会将user对象转换为对应的json字符串（jackson）
    }

}
